/**
 * 功能描述 行编辑
 */

define(function (require, exports, module){
	var Grid=require("grid");
	var store =require("store");
	var activeUser = store.get("user");
	var aw = require("ajaxwrapper");
	
	var instance = {
			initComponent: function (params,widget){
    			var appGrid = new Grid({
    				parentNode:".J-appGrid",
    				url:"api/approvaldefineitem/queryall",
            		autoRender:false,
    				model:{
    					head:{
    						title:"配置明细", 
    						buttons:[{
    							id:"add",
    							icon:"icon-plus",
    							handler:function(){
    								widget.hide(".J-appGrid").show(".J-appForm");
    								widget.get("appForm").reset();
    							}
    						}]
    					},
    					columns:[{
    						key : "approvalDefineItem.sequenceNumber",
    						name : "序号",
    					},{
    						key :"approvalDefineItem.approvalRole.name",
    						name : "审批角色",
    					},{
    						key:"personalInfo.name",
    						name:"审批人",
    					},{
    						key:"operate",
    						name:"操作",
    						format:"button",
    						formatparams:[{
    							id:"appedit",	
    							icon:"icon-edit",
    							show:function(value,row){
    								if(widget.get("flag")){
    									return true;
    								}else{
    									return false;
    								}
    							},
    							handler:function(index,data,rowEle){
    								widget.hide(".J-appGrid").show(".J-appForm");
    								var form = widget.get("form")
    								var appform = widget.get("appForm");
    								appform.reset();
    								appform.setValue("pkApprovalDefineItem",data.approvalDefineItem.pkApprovalDefineItem);
    								appform.setValue("version",data.approvalDefineItem.version);
    								appform.setValue("sequenceNumber",data.approvalDefineItem.sequenceNumber);
    								appform.setValue("approvalRole",data.approvalDefineItem.approvalRole.pkRole);
    								aw.ajax({
    									url:"api/attendant/queryattendant",
    									data:{
    										"commonUser.operator.roles.pkRole":data.approvalDefineItem.approvalRole.pkRole,
    										"organization.pkOrganization":activeUser.organization.pkOrganization,
    										fetchProperties:"*,personalInfo.*"
    										},
    									dataType:"json",
    									success:function(datas){
    										var temp = [];
    										if(datas.length>0){
    											for(var i in datas){
    												var dat = {};
    												dat.pkPersonalInfo = datas[i].personalInfo.pkPersonalInfo;
    												dat.name = datas[i].personalInfo.name;
    												temp.push(dat);
    											}
    										}
    										appform.setData("approvalUser",temp);
    										for(var j in datas){
    											if(datas[j].personalInfo.pkPersonalInfo == data.personalInfo.pkPersonalInfo){
    												appform.setValue("approvalUser",datas[j].personalInfo.pkPersonalInfo);
    												break;
    											}
    										}
    									}
    								});
    							}
    						},{
    							id:"appdelete",	
    							icon:"icon-remove",
    							show:function(value,row){
    								if(widget.get("flag")){
    									return true;
    								}else{
    									return false;
    								}
    							},
    							handler:function(index,data,rowEle){
    								aw.del("api/approvaldefineitem/"+data.approvalDefineItem.pkApprovalDefineItem+"/delete",function(data){
    									appGrid.refresh({
    										"approvalDefine.pkApprovalDefine":widget.get("pkApprovalDefine"),
    			            				fetchProperties:"approvalDefineItem.*," +
    			            						"personalInfo.*," +
    			            						"approvalDefineItem.approvalRole.*," +
    			            						"approvalDefineItem.approvalUser.*" ,
    									});
    								})
    							}
    						}]
    					}]
    				}
    			})
				widget.set("appGrid",appGrid);
			}
	};
	module.exports = instance;
});
